<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" :disabled="disabled" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" :disabled="disabled || dataForm.id !== ''" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" :disabled="disabled" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="广告词" prop="ad">
        <el-input v-model="dataForm.ad" :disabled="disabled" placeholder="广告词"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
<!--        <el-input v-model="dataForm.status" :disabled="disabled" placeholder="类型 1=上架 2=下架"></el-input>-->
        <el-radio-group v-model="dataForm.status">
          <el-radio :label=1>上架</el-radio>
          <el-radio :label=2>下架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="封面" prop="cover">
        <el-input v-model="dataForm.cover" :disabled="disabled" placeholder="封面"></el-input>
      </el-form-item>
      <el-form-item label="跳转地址,id为商品分类id" prop="link">
        <el-input v-model="dataForm.link" :disabled="disabled" placeholder="跳转地址"></el-input>
      </el-form-item>
      <el-form-item label="展示位置" prop="showLocation">
        <el-dict code="SHOW_LOCATION" :disabled="disabled" v-model="dataForm.showLocation"></el-dict>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: 0,
          sort: '',
          title: '',
          link: '',
          remark: '',
          ad: '',
          status: '',
          cover: '',
          createTime: '',
          updateTime: '',
          showLocation: ''},
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/modulecategory/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.modulecategory
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/modulecategory/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
